<?php

namespace App\Http\Controllers;

use App\Models\Comment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use TencentCloud\Sms\V20190711\SmsClient;
use TencentCloud\Sms\V20190711\Models\SendSmsRequest;
use TencentCloud\Common\Exception\TencentCloudSDKException;
use TencentCloud\Common\Credential;
use TencentCloud\Common\Profile\ClientProfile;
use TencentCloud\Common\Profile\HttpProfile;
use DB;

class HomeController extends Controller
{
    public function welcome() 
    {
        return view('welcome',[
            'recommends'=>$this->getRecommends(),
            'comments'=>$this->getComments(),
        ]);
    }

    public function getSmsCode(Request $request)
    {
        $phone = $request->input('phone');
        if (!$this->isPhone($phone)) {
            return response()->json([
                'state' => false, 
                'message' => 'phone is error!'
            ]);
        }
        $phone = $this->fixPhone($phone);

        $sms_code = mt_rand(1000, 9999);
        $expire_time = 10; //单位分钟

        try {
                
            $cred = new Credential(config('app.sms.secret_id'), config('app.sms.secret_key'));
            $httpProfile = new HttpProfile();
            $httpProfile->setReqMethod("GET"); 
            $httpProfile->setReqTimeout(30); 
            // $httpProfile->setEndpoint("sms.tencentcloudapi.com");
            $clientProfile = new ClientProfile();
            $clientProfile->setSignMethod("TC3-HMAC-SHA256");
            $clientProfile->setHttpProfile($httpProfile);
            $client = new SmsClient($cred, "ap-shanghai", $clientProfile);

            $req = new SendSmsRequest();
            $req->SmsSdkAppid = "1400442613";
            $req->Sign = "一点网络";
            $req->PhoneNumberSet = [$phone];
            $req->SenderId = "";
            $req->TemplateID = "762401";
            $req->TemplateParamSet = array($sms_code, $expire_time);
            $client->SendSms($req);

            $request->session()->put('sms_code_' . $phone, 'value');

            return response()->json([
                "state" => true,
            ]);
        }catch(TencentCloudSDKException $e) {
            Log::error($e->getMessage());

            return response()->json([
                "state" => false,
                "message" => $e->getMessage()
            ]);
        }
    }



    public function isPhone($phone) 
    {
        if (strlen($phone) <= 11) {
            return false;
        } else {
            return true;
        }
    }

    public function fixPhone($phone, $code = "+86")
    {
        if (strpos($phone, $code) !== 0) {
            return $code . $phone;
        } else {
            return $phone;
        }
    }

    public function getComments(){
    $data = [];
    Comment::limit(6)->get()->each(function ($model)use(& $data){
        $data[] = [
            'title' => $model->title,
            'head_img' => $model->head_img,
            'content' => $model->content,
        ];
    });
    return $data;
}

    public function getRecommends(){
        return [
            ['title'=>'','img'=>'/images/2-1.png','info'=>'#'],
        ];
    }
}
